package com.qf.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.blog.commons.exception.BusinessException;
import com.qf.blog.commons.security.UserTokenThreadLocal;
import com.qf.blog.commons.utils.SensitiveWordDetector;
import com.qf.blog.dto.SensitiveWordResp;
import com.qf.blog.entity.Comment;
import com.qf.blog.enums.ExceptionEnum;
import com.qf.blog.mapper.CommentMapper;
import com.qf.blog.service.*;
import com.qf.blog.vo.CommentVo;
import com.qf.blog.vo.LikeVo;
import com.qf.blog.vo.UserToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Autowired
    IUserService iUserService;
    @Autowired
    IPostService iPostService;
    @Autowired
    LikeService likeService;

    @Autowired
    private IPostScoreService postScoreService;

    @Autowired
    private SensitiveWordDetector SWDetector;

    @Override
    public boolean save(Comment comment) {
        String content = comment.getContent();
        SensitiveWordResp detect = SWDetector.detect(content);
        log.debug("发帖内容" + content);
        log.debug("返回结果" + detect);
        String detectStatus = detect.getStatus();
        if ("02".equals(detectStatus)) {
            throw new BusinessException(ExceptionEnum.SENSITIVE_COMMENT);
        }
        int insert = baseMapper.insert(comment);
        // 入库评论信息
        if (insert > 0) {
            //  帖子评论需要计算帖子的分数
            postScoreService.addPostIdToRedis(comment.getPostId());
            return true;
        }
        return false;
    }

    @Override
    public List<CommentVo> queryById(Integer postId) {
        List<CommentVo> commentList = baseMapper.queryByPostId(postId);
        List<CommentVo> newCommentList = createNewList(commentList);
        return newCommentList;
    }

    @Override
    public Long queryCommentCountByPostId(Integer postId) {
        QueryWrapper<Comment> qw = new QueryWrapper<>();
        qw.eq("post_id", postId);
        Long count = baseMapper.selectCount(qw);
        return count;
    }

    public List<CommentVo> createNewList(List<CommentVo> commentList) {
        List<CommentVo> commentListTypeP = new ArrayList<>();
        List<CommentVo> commentListTypeC = new ArrayList<>();
        for (CommentVo comment : commentList) {
            Comment commentEntity = comment.getCommentEntity();
            if (commentEntity.getEntityType().equals(1)) {
                commentListTypeP.add(comment);
            } else {
                commentListTypeC.add(comment);
            }
        }
        for (CommentVo commentP : commentListTypeP) {

            setCommentLike(commentP);
            List<CommentVo> list = new ArrayList<>();

            for (CommentVo commentC : commentListTypeC) {
                Comment commentEntity = commentC.getCommentEntity();

                setCommentLike(commentC);

                if (commentP.getCommentEntity().getId().equals(commentEntity.getEntityId())) {
                    list.add(commentC);
                }
            }
            commentP.setReplyList(list);
        }
        return commentListTypeP;
    }

    public void setCommentLike(CommentVo comment) {
        Comment commentEntity = comment.getCommentEntity();
        LikeVo likeVo = new LikeVo();
        likeVo.setEntityType(2);//回复和评论固定是2
        likeVo.setEntityId(commentEntity.getId());
        Long likeCount = likeService.getLikeCount(likeVo);
        comment.setLikeCount(likeCount);
        UserToken userToken = UserTokenThreadLocal.get();
        if (userToken == null) {
            comment.setLikeStatus(false);
        } else {
            Integer userId = userToken.getUserId();
            likeVo.setUserId(userId);
            boolean likeStatus = likeService.getLikeStatus(likeVo);
            comment.setLikeStatus(likeStatus);
        }
    }
}
